package com.lzp.appexp.main

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.net.Uri
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import com.base.Constants
import com.utils.LogUtils

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/3/13
 */
class MainActivityNew : BaseMainActivity() {

    private val TAG = "MainActivityNew==========="
    companion object {
        const val INDEX = "index"

        fun startMainActivity(context: Context, index: Int = 0) {
            val intent = Intent(context, MainActivityNew::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            val bundle = Bundle()
            bundle.putInt(INDEX, index)
            intent.putExtra(Constants.EXTRA_BUNDLE, bundle)
            context.startActivity(intent)
        }
    }

    private var index = 0

    override fun readArgument(bundle: Bundle) {
        super.readArgument(bundle)
        index = bundle.getInt(INDEX, index)
    }

    override fun writeArgument(bundle: Bundle) {
        super.writeArgument(bundle)
        bundle.putInt(INDEX, index)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //window.navigationBarColor = 0xffff0000.toInt()

        Log.d(TAG, "onCreate: ${intent.getStringExtra("666")}")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onNewIntent:")
    }
    override fun onNewIntent(intent: Intent?) {
        Log.d(TAG, "onNewIntent:")
        super.onNewIntent(intent)
    }
    override fun initView() {
        super.initView()
        setSelectedIndex(index)
        onTabChange(-1, index)

        val uri = Uri.parse("GwmAppMenu://" + "com.gwm.appmenu" + "/phantom?a=12&b=cde&a.b=c")
        val scheme = uri.scheme
        val host = uri.host
        val path = uri.path
        val query = uri.query

        LogUtils.e(scheme)
        LogUtils.e(host)
        LogUtils.e(path)
        LogUtils.e(query)
        for (queryParameterName in uri.queryParameterNames) {
            LogUtils.e(queryParameterName)
        }

        try {
            Thread(){
                run {
                    try {
                    throw IllegalStateException("6666666666666")
                    }catch (e:IllegalStateException){
                        e.printStackTrace()
                    }
                }
            }.start()
        }catch (e:IllegalStateException){
            e.printStackTrace()
        }

/*
        NetUtils
                .get()//.postFile()  .postForm() .postJson()
                .url("")
                .addHeader(map)
                .addParam("param1","")
                .addParam("param2","")//也可以.addParams(map),直接传map过去
                .build()
                .enqueue(callBack = null)// .execute() 也可以这样，这个是同步方式，enqueue是入队列，异步回调*/


        /*ImageLoader.get(this)
                .load("https://goss.veer.com/creative/vcg/veer/1600water/veer-375318408.jpg")
                .listener(object :ImgLoadListener.LoadListener{
                    override fun success(t: File?) {
                        Log.d("MainActivity",t?.absolutePath?:"")
                    }

                    override fun fail(e: Exception?) {
                        Log.d("MainActivity","fail+${e?.message}")
                    }
                })
                .start()*/

       /* val intent = Intent(this, TestService::class.java)
        bindService(intent, conn, Context.BIND_AUTO_CREATE)
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            startForegroundService(intent)
        }*/
    }

    override fun onDestroy() {
       // unbindService(conn)
        super.onDestroy()
    }

    private var startTime = System.currentTimeMillis()

    private val conn = object : ServiceConnection{
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            Log.d(TAG, "onServiceConnected:开始时间 ${(System.currentTimeMillis() - startTime) / 1000} 秒")
        }

        override fun onServiceDisconnected(name: ComponentName?) {
        }

    }
}